package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class JDBCDemo9 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        System.out.println("请输入id：");
        int id = scanner.nextInt();
        try (Connection connection = DButil.getConnection()) {
            connection.setAutoCommit(false);
            String sql = "UPDATE user SET password = ? WHERE id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, password);
            ps.setInt(2, id);
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "修改成功！" : "修改失败！");
            connection.commit();
            System.out.println("事务提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        scanner.close();
    }
}
